//
//  SecondController.m
//  WalkiOS
//
//  Created by luqiuan on 2019/10/12.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "SecondController.h"
#import "WAOrderListController.h"
#import "WAOrderPopView.h"
#import "WAOrderSheetView.h"
#import "WAGoodsListViewModel.h"

@interface SecondController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *sellButton;

@property (weak, nonatomic) IBOutlet UIButton *buyButton;
@property (weak, nonatomic) IBOutlet UIView *bottomLineLB;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomLineLeftCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollViewWidthCons;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (strong, nonatomic) WAOrderListController *buyOrderListVC;
@property (strong, nonatomic) WAOrderListController *sellOrderListVC;
@end

@implementation SecondController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.scrollViewWidthCons.constant = WNScreenWidth * 2;
    
    self.buyOrderListVC = [[WAOrderListController alloc] initWtihType:0];
    [self.scrollView addSubview:self.buyOrderListVC.view];
    [self addChildViewController:self.buyOrderListVC];
    self.buyOrderListVC.view.backgroundColor = ThemeColor;
    [self.buyOrderListVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.equalTo(self.scrollView);
        make.width.mas_equalTo(WNScreenWidth);
    }];
    
    self.sellOrderListVC = [[WAOrderListController alloc] initWtihType:1];
    [self.scrollView addSubview:self.sellOrderListVC.view];
    [self addChildViewController:self.sellOrderListVC];
    self.sellOrderListVC.view.backgroundColor = ThemeColor;
    [self.sellOrderListVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self.scrollView);
        make.width.mas_equalTo(WNScreenWidth);
    }];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.loginManager updateUserInfo:nil];
}

- (IBAction)sendOderAction:(id)sender {
    [self TFresignFirstResponder];
    WAOrderPopView *popView = [WAOrderPopView new];
    [popView setShowType:WNOperateViewShowTypeAlert];
    [popView show];
    [popView  setOrderClickBlock:^(int type) {
        if (!self.loginManager.isAuthentication) {
            [WNTool show:@"请先完善个人资料"];
            return;
        }
        //VIP0 不能发布买单和购买
        if (!self.loginManager.canPublish && type == 0) {
            [WNTool show:@"VIP0不能发布买单"];
            return;
        }
        WAOrderSheetView *pop = [WAOrderSheetView new];
        pop.type = type;
        [pop show];
        [pop setOrderClickBlock:^(int type, CGFloat number, NSString * price) {
            [self publishWtihType:type number:number price:price];
        }];
    }];
}

- (void)publishWtihType:(NSInteger)type number:(NSInteger)number price:(NSString *)price{
    [WNTool showLoadingMessage:@"正在发布..."];
    WATransactionModel *model = [WATransactionModel new];
    model.transactiontype = type;
    model.number = [@(number) stringValue];
    model.price = price;
    [WNBusinessRequest publishTransaction:model Succ:^(id  _Nonnull responseObject) {
        [WNTool show:@"发布成功"];
        [self.buyOrderListVC reload];
        [self.sellOrderListVC reload];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        [WNTool show:mes];
    }];
}

- (IBAction)buyBtnAction:(id)sender {
    [self TFresignFirstResponder];
    [self.buyOrderListVC viewWillAppear:NO];
    if (self.buyButton.selected) {
        return;
    }
    self.sellButton.selected = NO;
    self.buyButton.selected = YES;
    [UIView animateWithDuration:0.3 animations:^{
        self.bottomLineLeftCons.constant = 0;
        [self.view layoutIfNeeded];
    }];
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (IBAction)sellBtnAction:(id)sender {
    [self TFresignFirstResponder];
    [self.sellOrderListVC viewWillAppear:NO];
    if (self.sellButton.selected) {
        return;
    }
    self.sellButton.selected = YES;
    self.buyButton.selected = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.bottomLineLeftCons.constant = WNScreenWidth/2;
        [self.view layoutIfNeeded];
    }];
    [self.scrollView setContentOffset:CGPointMake(WNScreenWidth, 0) animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offset_X = scrollView.contentOffset.x;
    self.bottomLineLeftCons.constant = offset_X/2;
    [self.view layoutIfNeeded];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat constant = 0;
    if (scrollView.contentOffset.x == 0) {
        constant = 0;
    }else{
        constant = WNScreenWidth/2;
    }
    self.sellButton.selected = !(scrollView.contentOffset.x == 0);
    self.buyButton.selected = (scrollView.contentOffset.x == 0);
}

- (void)TFresignFirstResponder{
    
    UITextField *tf = [self.view.window performSelector:@selector(firstResponder)];
    [tf resignFirstResponder];
}
@end
